﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using LibNiconico.NiconicoApi;
using LibNiconico.NiconicoApi.Lives.Comments;
using NicoApiHelperTest.Comments;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Input;

namespace NicoApiHelperTest.ViewModel
{
    public class TestCommentViewModel:ViewModelBase
    {
        private const string mail = "riot313@gmail.com";
        private const string password = "shell937";
        //private const string mail = "nicotestuser@mail.goo.ne.jp";
        //private const string password = "niconico";

        private INicoCommentClient commentClient;

        private NicoApiHelper helper;

        public ObservableCollection<NicoChatComment> ChatComments { get; private set; }

        internal INicoCommentClient CommentClient
        {
            get
            {
                return commentClient;
            }

            set
            {
                commentClient = value;
                commentClient.Received += onReceive;
            }
        }

        private void onReceive(IEnumerable<NicoChatComment> obj)
        {
            foreach (var readComment in obj)
            {
                ChatComments.Add(readComment);
            }
        }

        static TestCommentViewModel()
        {
            liveRegex = new Regex("((lv)|(ch)|(co))([0-9]+)");
        }

        public TestCommentViewModel()
        {
            commentClient = null;

            ChatComments = new ObservableCollection<NicoChatComment>();

            helper = new NicoApiHelper();

            connectCommand = null;
        }



        public  async Task LoginAsync()
        {
            await this.helper.LoginAsync(mail, password);
        }
        

        private RelayCommand<string> connectCommand;

        private RelayCommand<string> sendMessageCommand;

        private RelayCommand loginCommand;

        public ICommand ConnectCommand
        {
            get
            {
                if (connectCommand == null)
                {
                    connectCommand = new RelayCommand<string>(connection);
                }

                return connectCommand;
            }
        }

        public ICommand LoginCommand
        {
            get
            {
                if (loginCommand == null)
                {
                    loginCommand = new RelayCommand(async () =>
                    {
                        await LoginAsync();
                    });
                }

                return loginCommand;
            }

        }

        public ICommand SendMessageCommand
        {
            get
            {
                if (sendMessageCommand == null)
                {
                    sendMessageCommand = new RelayCommand<string>(sendOwnerMesage);
                }

                return sendMessageCommand;
            }
        }

        private async void connection(string id)
        {
            var liveMatch= liveRegex.Match(id);
            if (liveMatch.Success)
            {
                var liveResponse = await helper.LiveApi.ConnectLiveAsync(liveMatch.Value);

                CommentClient = TestNicoCommentClientFactry.CreateFrom(liveResponse.CommentClient);
                await CommentClient.ConnectAsync();
                
            }
        }


        private async void sendMessage(string message)
        {
            var sendChatComment=new NicoSubmitChatComment();
            sendChatComment.Body = message;
            await this.commentClient.SendAsync(sendChatComment);
        }


        private async void sendOwnerMesage(string message)
        {
            var ownerChatComment = new NicoLiveOwnerChatComment();
            ownerChatComment.Body = message;
            await this.commentClient.OwnerSendAsync(ownerChatComment);
        }


        private static Regex liveRegex;
    }
}
